#include <iostream>
#include <cstring>
#include <unordered_map>
#include <queue>
using namespace std;

unordered_map<char,int> has;
int main()
{
	string str;
	
	while(cin>>str)
	{
		for(int i=0;i<str.size();i++)
		{
			has[str[i]]++;
		}
		priority_queue<int,vector<int>,greater<int>> q;
		
		for(auto&[x,y]:has)
		{
			q.push(y);
		}
		int sum=0;
		while(q.size()>1)
		{
			int t1=q.top();
			q.pop();
			int t2=q.top();
			q.pop();
			sum+=t1+t2;
			q.push(t1+t2);
		}
		
		cout<<sum<<endl;
		has.clear();
		
	}
	
	return 0;
}
